<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;
use kartik\file\FileInput;
use kartik\editable\Editable;
use app\modules\Basic\models\CityCode;
use app\modules\Manage\models\User;

//获取城市
$city = CityCode::find()->all();
$citys = [];
foreach($city as $ct){
    $citys[$ct->code] = $ct->code.'——'.$ct->en_name.'——'.$ct->zh_name;
}  

//获取操作
// $user = User::find()->all();
// $users = [];
// foreach($user as $us){
//     $users[$us->id] = $us->name;
// }  

$users = \yii::$app->role->getAllOper();




$this->title = '团队详情';

$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Products'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

?>
<style type="text/css">
   .content-view{}
   .content-view .row{margin: 0;}
   .content-view .view-one{margin:5px;}
   .upload-file{margin-top: 20px;}
   .upload-file-button{float: right;}
   .offer-file{margin-top: 15px;}
   .cs1{margin:5px;} 
</style>
<div class="product-view">

   <div class="box">
        <div class="box-header">

        </div>
        <div class="box-body"> 
            <div class="content-view">
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>客户名称：</label><?=$model->customer->name?>
                    </div> 
                    <div class="col-md-4">
                        <label>客户团号：</label>
                        <?php echo Editable::widget([
                            'name' => 'Product[customer_code]',
                            'value' => $model->customer_code,
                            'asPopover' => true,
                            'header' => '客户团号',
                            'size'=>'md',
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => ['class'=>'form-control', 'placeholder'=>'客户团号'],
                        ]);?>
                    </div> 
                </div>    
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>客户账期：</label><?=$model->customer->payment?>
                    </div> 
                    <div class="col-md-4">
                        <label>团队类别：</label>
                       
                        <?php echo Editable::widget([
                                'name'=>'Product[product_type]',
                                'header' => '团队状态',
                                'displayValue' => Yii::$app->params['product_type'][$model->product_type],
                                'value' => Yii::$app->params['product_type'][$model->product_type],
                                'formOptions' => ['action'=>Url::toRoute(['/product/product/edit-type','Product[id]'=>$model->id]),'method'=>'get'],
                                'format' => Editable::FORMAT_BUTTON,
                                'inputType' => Editable::INPUT_DROPDOWN_LIST,
                                'data'=> Yii::$app->params['product_type'], 
                                'options' => ['class'=>'form-control'],
                                'editableValueOptions' => ['class'=>'text-danger']
                            ]);

                        ?>
                    </div> 
                </div>  
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>团队名称：</label>
                        <?php echo Editable::widget([
                            'name' => 'Product[name]',
                            'value' => $model->name,
                            'asPopover' => true,
                            'header' => '团队名称',
                            'size'=>'md',
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => ['class'=>'form-control', 'placeholder'=>'团队名称'],
                        ]);?>
                    </div>  
                    <div class="col-md-4">
                        <label>团号：</label>
                        <?php echo Editable::widget([
                            'name' => 'Product[code]',
                            'value' => $model->code,
                            'asPopover' => true,
                            'header' => '团号',
                            'size'=>'md',
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => ['class'=>'form-control', 'placeholder'=>'团号'],
                        ]);?>
                    </div>     
                </div> 
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>联系人：</label><?=$model->p_name?>
                    </div> 
                    <div class="col-md-4">
                        <label>联系人电话：</label><?=$model->p_phone?>
                    </div>  
                </div> 
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>联系人邮箱：</label><?=$model->p_mail?>
                    </div>     
                    <div class="col-md-4">
                        <label>联系人部门：</label><?=$model->p_department?>
                    </div>  
                </div> 
                <div class="row view-one">
                    <div class="col-md-4">
                          <label>目的地城市：</label><?php echo Editable::widget([
                            'name' => 'Product[city]',
                            'value' => is_array(json_decode($model->city)) ? json_decode($model->city) : [],
                            //'data' => $citys,
                            'displayValue' => $model->cityInfo,
                            'asPopover' => true,
                            'header' => '目的地',
                            'inputType' => '\kartik\select2\Select2',
                            'size'=>'md',
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit-city','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => [
                                'class'=>'form-control',
                                'data' => $citys,
                                'options' => [
                                   'multiple' => true,
                                ],
                            ],
                        ]);?>
                    </div>  
                    <div class="col-md-4">
                        <label>抵离日期：</label><?=$model->begin_date.' / '.$model->end_date?>
                    </div>    
                </div>    
                <div class="row view-one">
                    <div class="col-md-4">
                        <label>团人数：</label><?=$model->num?> 人
                    </div> 
                    <div class="col-md-4">
                        <label>状态：</label>
                        <?php if(in_array($model->status,[1,2,3])){ ?>
                        <?php 
                         $product_status = [1 => '报价', 2 => '成团', 3 => '执行',];
                         echo Editable::widget([
                                'name'=>'Product[status]',
                                'header' => '团队状态',
                                'displayValue' => Yii::$app->params['product_status'][$model->status],
                                'value' => Yii::$app->params['product_status'][$model->status],
                                'formOptions' => ['action'=>Url::toRoute(['/product/product/edit-status','Product[id]'=>$model->id]),'method'=>'get'],
                                'format' => Editable::FORMAT_BUTTON,
                                'inputType' => Editable::INPUT_DROPDOWN_LIST,
                                'data'=> $product_status, 
                                'options' => ['class'=>'form-control'],
                                'editableValueOptions' => ['class'=>'text-danger']
                            ]);
                         ?>
                    <?php }else{ ?>
                        <?= Yii::$app->params['product_status'][$model->status] ?>
                    <?php } ?>
                    </div> 
                </div> 

                <div class="row view-one">
                    <div class="col-md-4">
                        <label>总收入：</label><?=$model->price?>
                    </div>  
                    <div class="col-md-4">
                        <label>总成本：</label><?=$model->costPrice?>
                    </div>   
                </div>       

                <div class="row view-one">
                    <div class="col-md-4">
                        <label>实际总收入：</label><?=$model->realPrice?>
                    </div>  
                    <div class="col-md-4">
                        <label>汇款手续费：</label><?=$model->proPrice?>
                    </div>   
                </div>    

                <div class="row view-one">
                    <div class="col-md-4">
                        <label>毛利：</label><?=$model->profitPrice?>
                    </div>  
                    <div class="col-md-4">
                        <label>毛利率：</label><?=$model->profit ?>
                    </div>   
                </div>  


                <div class="row view-one">
                    <div class="col-md-4">
                        <label>已收金额：</label><?=$model->incomePrice?>
                    </div>  
                    <div class="col-md-4">
                        <label>欠款金额：</label><?=$model->price - $model->incomePrice ?>
                    </div>   
                </div>  

                <div class="row view-one">
                    
                    <div class="col-md-4">
                        <label>开团人：</label><?=$model->userName?>
                    </div>  
                    <div class="col-md-4">
                        <label>指定操作：</label><?php echo Editable::widget([
                            'name' => 'Product[oper_id]',
                            'value' => json_decode($model->oper_id),
                            //'data' => $citys,
                            'displayValue' => $model->operName,
                            'asPopover' => true,
                            'header' => '操作人',
                            'inputType' => '\kartik\select2\Select2',
                            'size'=>'md',
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit-oper','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => [
                                'class'=>'form-control',
                                'data' => $users,
                                'options' => [
                                   'multiple' => true,
                                ],
                            ],
                        ]);?>
                    </div>     
                </div> 

                <div class="row view-one">
                    <div class="col-md-4">
                        <label>创建时间：</label><?=$model->created_at?>
                    </div> 
                    <div class="col-md-4">
                        <label>更新时间：</label><?=$model->updated_at?>
                    </div>  
                       
                </div> 

                <div class="row view-one">
                    <div class="col-md-4">
                        <label>备注：</label><?php echo Editable::widget([
                            'name' => 'Product[remark]',
                            'value' => $model->remark,
                            'asPopover' => true,
                            'header' => '备注信息',
                            'inputType' => Editable::INPUT_TEXTAREA,
                            'formOptions' => ['action'=>Url::toRoute(['/product/product/edit','Product[id]'=>$model->id]),'method'=>'get'],
                            'options' => ['class'=>'form-control', 'placeholder'=>'请输入备注信息'],
                        ]);?>
                    </div> 
                </div> 

            </div>  
            <?php if($model->user_id == \yii::$app->user->id){ ?>   
            <div class="upload-file">
                <div class="upload-file-button">
                    <?php echo FileInput::widget([
                      'language' => 'zh',
                      'name' => 'excel',
                      'id' => 'file-upload',
                      'pluginOptions' => [
                          'uploadUrl' =>Url::toRoute(['/product/product/upload-file']),
                          'uploadExtraData' => [
                              'product_id' => $model->id,  
                              '_csrf' => Yii::$app->getRequest()->getCsrfToken(),
                          ],
                          'showPreview' => false,
                          'showCaption' => false,
                          'showRemove' => false,
                          'showUpload' => false,
                          'uploadAsync'=> true,
                          'maxFileCount' => 1,
                          'browseClass' => 'btn btn-primary',
                          'browseIcon' => '',
                          'browseLabel' => '上传报价单',
                          'fileTypeSettings' => [],
                      ],
                      'options' => ['accept' => '', 'multiple'=>false],
                      ]);?>
                </div>    
            </div>
            <?php }?>  

            <div style="clear: both;"></div>

            <div class="offer-file">
                <h4>报价单</h4>
                <table class="table table-bordered text-center">
                    <thead>
                        <tr>
                            <td>报价单文件名</td>
                            <td>报价总收入</td>
                            <td>次数</td>
                            <td>上传时间</td>
                            <td>操作</td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if($model->allLogs){ ?>
                            <?php foreach($model->allLogs as $key=>$log){ ?>
                                <tr>
                                    <td><?=$log->name?></td>
                                    <td><?=$log->price?></td>
                                    <td><?=$key+1?></td>
                                    <td><?=$log->created_at?></td>
                                    <td><a class="cs1 btn btn-success btn-xs" href="/product/log/download?id=<?=$log->id?>" >下载</a><?php if($model->user_id == \yii::$app->user->id){ ?><a class="cs1 btn btn-danger btn-xs del-file" data-id="<?=$log->id?>" >删除 </a><?php }?></td>
                                </tr>
                            <?php  }?>
                        <?php }?>
                    </tbody>
                </table>
            </div>    

        </div>
    </div>      
</div>

<?=  yii\web\View::registerJsFile("/js/layer/layer.js",['depends' => 'app\assets\AppAsset']) ?>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">

$(document).ready(function () {
    $('#file-upload').on('filebatchselected', function(event, files) {
        $(this).fileinput("upload");
    });

    $('#file-upload').on('fileuploaded', function(event, data, previewId, index) {
        var response = data.response;
        if(response.code == 'error'){
            layer.msg(response.message,{time: 2000},function(){});
            setTimeout("window.location.reload()",2000);
            return false;
        }else if(response.code == 'ok'){
            layer.msg(response.message);
            window.location.reload();
            return false;
        }
    });

    $(".offer-file .del-file").click(function(){
        var msg = "您真的确定要删除吗?"; 
        if(confirm(msg) == false){ 
             return false;
        }
        var id = $(this).data("id");
        $.ajax({
           type: "get",
           async:false, 
           url: "<?=Url::toRoute(['/product/log/del-file'])?>",
           data: {'id':id},
           success: function(msg){
               var msg = $.parseJSON(msg);
               if(msg.code == 0){
                   layer.msg(msg.message);
                   window.location.reload();
               }else if(msg.code == 1){
                   layer.msg(msg.message);
                   window.location.reload();
               }
            }
       });

    });
});

</script>
<?php app\components\JsBlock::end() ?>  